package cate.game.play.skill.effect;


import cate.common.table.d.GD;
import cate.common.table.d.GDPlay;
import cate.game.attr.FightAttr;
import cate.game.play.fighter.Fighter;
import cate.game.play.fighter.HealResult;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.effect.base.HpSE;
import easy.java.dev.note.NoteClass;

@NoteClass("治疗SE")
public class 治疗SE extends HpSE {


    @Override
    public void settle(SkillActionCtx action, Fighter target) {
        AttrPair ap = AttrPair.calc(this, action, target);
        FightAttr actorAttr = ap.actor;
        FightAttr targetAttr = ap.target;
        double hpChange = calcHpChange(action, target, actorAttr, targetAttr);
        //仙术治疗
        hpChange += calXSHeal(action, actorAttr);
        //元素治疗
        hpChange += totalCoeff/GD.W_10000_0 * actorAttr.elementAttTotal(action.getActor().getCamp());
        //治疗增加和减免
        hpChange *= healRate(action.getActor(), target, actorAttr, targetAttr);
        //暴击处理
        boolean strike = false;
        double strikeRate = actorAttr.healStrike()/GD.W_10000_0;
        if (Math.random() < strikeRate) {
            strike = true;
            hpChange *= 1.3;
        }
        //技能衰减系数
        hpChange *= 1 + action.getMove().param.getHpChangeRate();
        hpChange *= 1 + parent.getHpChangeRate(target.getPid());

        final HealResult healResult = target.attr.heal(action, action.getActor(), hpChange, strike, GDPlay.HpReason.SKILL_HEAL);
        action.getActor().buff.forHandlers(a -> a.onActiveHeal(action, target, healResult));
        target.buff.forHandlers(a -> a.onActiveHealAsTarget(action, healResult));
    }

    private double healRate(Fighter actor, Fighter target, FightAttr actorAttr, FightAttr targetAttr) {
        double rate = 1.0;
        rate += (actorAttr.healUp() - targetAttr.healedDown()) / GD.W_10000_0;
        //治疗他人时 享受两个人的治疗属性
        if (actor.getPid() != target.getPid()) {
            rate += targetAttr.healUp() / GD.W_10000_0;
        }
        return Math.max(0.1d, rate);
    }

    /**
     * 计算仙术治疗
     * @param action
     * @param actorAttr
     * @return
     */
    protected double calXSHeal(SkillActionCtx action, FightAttr actorAttr) {
        if (!action.getActor().status.isAwaken()) {
            return 0d;
        }
        double value = actorAttr.psyAttTotal();
        double rate = 1 + actorAttr.psyUp() / GD.W_10000_0;
        rate = Math.max(0, rate);
        return value * rate * xsTargetCoeff(action.getTargets().size());
    }
}
